package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.tapestry5.model.ValueLabel;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.common.EMailAddress;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.EMailAddressService;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.HlogConstants;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.ValueLabelUtil;
import com.ptswitch.material.util.security.PermissionCheckUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class EditMaterial extends PageBase {

    @InjectPage
    private ViewMaterial viewMaterial;
    @Inject
    private MaterialService materialSer;
    @Inject
    private EMailAddressService emailAddressSer;
    // -------------------------
    @Property
    private Material material;
    @Property
    private EMail email;
    @Property
    private List<EMailAddress> emailAddresses;
    @Property
    private ValueLabel<EMailAddress> emailAddressLabel;
    // -------------------------
    @Property
    private boolean isNewPcbDecal;
    @Property
    private boolean materialMoreButton;
    @Property
    private boolean emailMoreButton;

    // ---------------------------------
    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        checkPermitted(PermissionCheckUtil.permittedAny(PermissionUtil.MaterialEdit));
        material = materialSer.findById(id);
        isNewPcbDecal = false;
        materialMoreButton = false;
        emailMoreButton = false;
        email = MaterialUtil.newEMail();
        emailAddresses = emailAddressSer.findAll();
        emailAddressLabel = ValueLabelUtil.emailAddressLabel;
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return material.getId();
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "update")
    Object update() {
        checkPermitted(PermissionCheckUtil.permittedAny(PermissionUtil.MaterialEdit));
        materialSer.save(material);
        // send mail
        for (EMailAddress address : emailAddresses) {
            email.addTo(address.getEmail());
        }
        email.setSubject(getVisitor().getRealname() + HlogConstants.updateMaterial);
        email.addText(email.getTmpTextContent());
        email.addHtml(MaterialUtil.linkForViewMaterial(getWebUrl(), material));
        email.addText(MaterialUtil.buildMessage(material));
        sendEMail(email);
        hlog(HlogConstants.updateMaterial, material);
        return viewMaterial.withInit(material.getId());
    }

}
